package com.ming.MathTest;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Auther: tangC
 * @Date: 2020/7/14 09:58
 * @Description:
 */
@Slf4j
public class BinarySearch {

    public static void main(String[] args) {
        int [] x = {0, 4, 6, 6, 7, 8, 13, 13, 16};
        List<Integer> list = new ArrayList<Integer>();
        for (int a:x){
            list.add(a);
        }
        Collections.sort(list);

        // 查找小于number最大数
        int number = 6;
        int findNumber = 0;
        int low = 0;
        int high = list.size() - 1;
        while (low < high) {
            int mid = (low + high) / 2;
            int temp = list.get(mid);
            if (temp < number) {
                findNumber = temp;
                low = mid+1;
            } else if (temp > number) {
                high = mid;
            } else {
                high = mid;
            }
            if (high == low){
                break;
            }
        }
        log.info("{}", new Integer(findNumber));
        log.info(list.toString());
    }
}
